package com.simpletour.qa.gateway.util;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Charsets;
import com.netflix.zuul.context.RequestContext;
import com.simpletour.qa.common.http.ObjectRestResponse;
import org.apache.http.entity.ContentType;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhaohw
 * @date 2018-06-21 10:21
 */
public class HttpContextUtil {

    /**
     * 发送http status 200 的Response
     * @param ctx
     * @param body
     * @return
     */
    public static HttpServletResponse sendOkResponse(RequestContext ctx , ObjectRestResponse body){
        HttpServletResponse response = ctx.getResponse();
        response.setStatus(HttpStatus.OK.value());
        response.setContentType(ContentType.APPLICATION_JSON.getMimeType());
        response.setCharacterEncoding(Charsets.UTF_8.name());
        try {
            response.getWriter().write(JSON.toJSONString(body));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}
